/* 
 * SinaInterface.cpp
 *  Created on: 2011-03-25
 *      Author: MillionLee
 */
#include "StdAfx.h"

#include <Python.h>
#include <boost\python.hpp>

#include "SinaInterface.h"

SinaInterface::SinaInterface(void)
{
}

SinaInterface::~SinaInterface(void)
{
}

bool SinaInterface::DoUpdate(const RePost &rePost)
{
	boost::python::handle<>* _module = NULL;
	bool result;

	try{
		Py_Initialize();   
		if (!Py_IsInitialized())
			return false;
		PyRun_SimpleString("import sys");   
		PyRun_SimpleString("sys.path.append('./SinaInterfacePy')");

		_module = new boost::python::handle<>(PyImport_ImportModule("SinaInterfacePy"));
	}
	catch(...){
		PyErr_Print();
		PyErr_Clear();
		delete _module;
		_module = NULL;
		return false;
	}

	//try to update with image
	try{
		if (rePost.get_imageNamePath() == "")
			result = boost::python::call_method<bool>(_module->get(), "DoUpdate", rePost.get_text());
		else
		{
			string imageNamePathPy = rePost.get_imageNamePath();
			string::size_type pos = imageNamePathPy.find('\\');
			while (pos != -1)
			{
				imageNamePathPy.replace(pos, 1, "/");
				pos = imageNamePathPy.rfind(pos, '\\');
			}
			result = boost::python::call_method<bool>(_module->get(), "DoUpdateWithImage", rePost.get_text(), imageNamePathPy);
		}
	}
	catch(...){
		PyErr_Print();
		PyErr_Clear();
	}

	return result;
}